<?php

abstract class Engine_Controller {

	protected $view;
    protected $activeUser;
    protected $menu = null;
    protected $name = null;

   	abstract public function indexAction();

   	public function __construct() {
        $this->activeUser = Application_Bootstrap::getActiveUser();
   		$this->view = new Engine_View();
   		$this->view->setTemplate($this->getDefaultTemplate());
   	}

    public function preDispatch() {
        $view = $this->getView();
        $view->moduleName = $this->getRequest()->module;
        if ($this->menu) { $view->setMenuParent($this->menu); }
        if ($this->name) { $view->setMenuName($this->name); }

    }

    /**
     * @return Engine_Http_Request
     */
    public function getRequest()
   	{
    	return Application_Bootstrap::getRequest();
   	}

    /**
     * @return Engine_View
     */
    public function getView() {
   		return $this->view;
   	}

   	public function run() {
   		$this->view->render();
   	}

   	private function getDefaultTemplate() {
   		$request = $this->getRequest();
   		$templatePath = $_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . '..' 
         . DIRECTORY_SEPARATOR . 'application' 
         . DIRECTORY_SEPARATOR . 'Module' 
         . DIRECTORY_SEPARATOR . ucfirst($request->module) 
         . DIRECTORY_SEPARATOR . 'View' 
         . DIRECTORY_SEPARATOR . ucfirst($request->controller) 
         . DIRECTORY_SEPARATOR . ucfirst($request->action) . '.phtml';
   		return $templatePath;
   	}

      public function setNoTemplate() {
         $this->view->setTemplate(null);
      }

    public function authUserOnly(){

        $activeUser=Application_Bootstrap::getActiveUser();
        if(!$activeUser->isAuthorized()){
            $this->getRequest()->redirect('/account/user/login/');
        }



    }
}